//
//  DatePickerViewController.m
//  TodaysPlan
//
//  Created by 寺田 篤史 on 11/10/14.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "DatePickerViewController.h"
#import "ConfigManager.h"

@implementation DatePickerViewController

@synthesize delegate;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *compButton = [[[UIBarButtonItem alloc] 
                                    initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                    target:self 
                                    action:@selector(completePickerDate:)] 
                                    autorelease];
    self.navigationItem.rightBarButtonItem = compButton;
    self.title = NSLocalizedString(@"reminder","reminder text");

    UIDatePicker *dp = [[[UIDatePicker alloc] init] autorelease];
    dp.center = self.view.center;
    dp.minuteInterval = 1;
    dp.datePickerMode = UIDatePickerModeTime;
    ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
    NSString *reminderDate = [config.cfgReminderDate getValueString];
    NSDateFormatter *inDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [inDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *inputDate = [inDateFormatter dateFromString:reminderDate] ? [inDateFormatter dateFromString:reminderDate] : [NSDate date];
    NSString *reminderDateStr = [inDateFormatter stringFromDate:inputDate]; 
    [dp setDate:inputDate animated:YES];
    [dp addTarget:self action:@selector(timeChange:) forControlEvents:UIControlEventValueChanged];
    [config.cfgReminderDate setValueString:reminderDateStr];
    [self.view addSubview:dp];
}


- (void)viewDidAppear:(BOOL)animated {
    //[picker setDate:[NSDate date] animated:NO];
    //[self update:nil];
}

- (void)completePickerDate:(id)sender {
	[self.navigationController popViewControllerAnimated:YES];
}
- (void)timeChange:(UIDatePicker *)dp{
    DLog(@"選んだ日時は: %@", [NSDateFormatter localizedStringFromDate:[dp date] 
                                                        dateStyle:NSDateFormatterNoStyle
                                                        timeStyle:NSDateFormatterShortStyle]);
    NSDateFormatter *inDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [inDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString* reminderDateStr = [inDateFormatter stringFromDate:[dp date]];    
    ConfigManager *config = [[[ConfigManager alloc] init] autorelease];
    [config.cfgReminderDate setValueString:reminderDateStr];
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}
@end
